package com.ruoyi.web.controller.module.wx;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.wx.UserAddress;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.module.wx.IUserAddressService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 用户地址Controller
 *
 * @author ruoyi
 * @date 2021-08-24
 */
@Validated
@Api(value = "用户地址控制器", tags = {"用户地址管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/wx/user/address")
public class UserAddressController extends BaseController {

    private final IUserAddressService userAddressService;

    /**
     * 查询用户地址列表
     */
    @ApiOperation("查询用户地址列表")
    @PreAuthorize("@ss.hasPermi('system:address:list')")
    @GetMapping("/list")
    public TableDataInfo<UserAddress> list(@Validated UserAddress userAddress) {
        return PageUtils.buildDataInfo(userAddressService.page(PageUtils.buildPage()));
    }

    /**
     * 导出用户地址列表
     */
    @ApiOperation("导出用户地址列表")
    @PreAuthorize("@ss.hasPermi('system:address:export')")
    @Log(title = "用户地址", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<UserAddress> export(@Validated UserAddress userAddress) {
        List<UserAddress> list = userAddressService.list();
        ExcelUtil<UserAddress> util = new ExcelUtil<UserAddress>(UserAddress.class);
        return util.exportExcel(list, "用户地址");
    }

    /**
     * 获取用户地址详细信息
     */
    @ApiOperation("获取用户地址详细信息")
    @PreAuthorize("@ss.hasPermi('system:address:query')")
    @GetMapping("/{id}")
    public AjaxResult<UserAddress> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        UserAddress userAddress = userAddressService.getById(id);
        return AjaxResult.success(userAddress);
    }

    /**
     * 新增用户地址
     */
    @ApiOperation("新增用户地址")
    @PreAuthorize("@ss.hasPermi('system:address:add')")
    @Log(title = "用户地址", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody UserAddress userAddress) {
        return toAjax(userAddressService.save(userAddress) ? 1 : 0);
    }

    /**
     * 修改用户地址
     */
    @ApiOperation("修改用户地址")
    @PreAuthorize("@ss.hasPermi('system:address:edit')")
    @Log(title = "用户地址", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody UserAddress userAddress) {
        return toAjax(userAddressService.updateById(userAddress) ? 1 : 0);
    }

    /**
     * 删除用户地址
     */
    @ApiOperation("删除用户地址")
    @PreAuthorize("@ss.hasPermi('system:address:remove')")
    @Log(title = "用户地址" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(userAddressService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
